﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.IO;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Net.Configuration;

namespace SES.CMS
{
    public partial class Contact : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 6;
            cmsCategoryDO objCat = new cmsCategoryDO();
            objCat.CategoryID = id;
            objCat = new cmsCategoryBL().Select(objCat);
            if (Session["lang"] != null)
            {
                if (Session["lang"].ToString() == "VN")
                {
                    Page.Title = objCat.Title + " - KMA";
                    lblInfo.Text = objCat.DetailContent;
                }
                else
                {
                    Page.Title = objCat.eTitle + " - KMA";
                    lblInfo.Text = objCat.eDetailContent;
                }
            }
            else
            {
                Page.Title = objCat.Title + " - KMA";
                lblInfo.Text = objCat.DetailContent;
            }
        }
        public static String Address
        {
            get
            {
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                return cfg.Network.UserName;
            }
        }
        public static String Pass
        {
            get
            {
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                return cfg.Network.Password;
            }
        }
        public void sendsMail(String name, String bd, string title)
        {
            string i = DateTime.Now.ToString();
            sysConfigDO cmsConfig = new sysConfigDO();
            int id = 3;

            cmsConfig = new sysConfigBL().Select(new sysConfigDO { ConfigID = 3 });

            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential(Address, Pass);
            SmtpServer.Port = 587;
            SmtpServer.Host = "Smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            MailMessage mail = new MailMessage();
            String addr = cmsConfig.ConfigValue.ToString();

            try
            {
                mail.From = new MailAddress(addr, name, System.Text.Encoding.UTF8);
                mail.To.Add(addr);
                mail.Subject = title;
                mail.Body = bd;
                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
            }
        }
        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtbv.Text);
            if (!ccJoin.UserValidated)
            {
                if (Session["lang"] != null)
                {
                    if (Session["lang"].ToString() == "VN")
                    {
                        Response.Write("<script language='javascript'>alert('Mã bảo vệ chưa chính xác..!!..')</script>");
                        return;
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('Security Code is incorrect..!!..')</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('Mã bảo vệ chưa chính xác..!!..')</script>");
                    return;
                }
            }
            else
            {
                try
                {
                    String ten = txtTen.Text;
                    String bd = txtND.Text;
                    String title = txtSub.Text;
                    sendsMail( ten, bd, title);
                    if (Session["lang"] != null)
                    {
                        if (Session["lang"].ToString() == "VN")
                        {
                            Ultility.Alert("Thành công!!!", "/Default.aspx");
                        }
                        else
                        {
                            Ultility.Alert("Successful!!!", "/Default.aspx");
                        }
                    }
                    else
                    {
                        Ultility.Alert("Thành công!!!", "/Default.aspx");
                    }
                }
                catch (Exception ee)
                {
                    lbLoi.Text = ee.ToString();
                }
            }
        }
    }
}
